package edu.buct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import core.api.BaseApiService;
import core.api.BaseResponse;

import edu.buct.entity.C2;
import edu.buct.mapper.C2Mapper;
import edu.buct.service.C2Service;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class C2ServiceImpl extends BaseApiService implements C2Service {

    @Resource
    private C2Mapper c2Mapper;

    @Override
    public BaseResponse<C2> selectAll() {
        // 开始查询
        QueryWrapper<C2> queryWrapper = new QueryWrapper<>();
        // column ->写在数据库字段，参数1的值==参数2的值

        List<C2> c1List = c2Mapper.selectList(queryWrapper);
        return setResultSuccessData(c1List);
    }

    @Override
    public BaseResponse insert(C2 c2) {
        QueryWrapper<C2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", c2.getId());
        C2 one = c2Mapper.selectOne(queryWrapper);
        if (one != null) {
            return setResultError("already exist");
        }
        return c2Mapper.insert(c2) > 0 ? setResultSuccess() : setResultError("insert fail");
    }

    @Override
    public BaseResponse delete(Integer id) {
        QueryWrapper<C2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        C2 one = c2Mapper.selectOne(queryWrapper);
        if (one == null) {
            return setResultError("not exist");
        }
        return c2Mapper.deleteById(id) > 0 ? setResultSuccess() : setResultError("delete fail");
    }

    @Override
    public BaseResponse<C2> updateInfo(C2 c2) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", c2.getId());
        return c2Mapper.update(c2, updateWrapper) > 0 ? setResultSuccess() : setResultError("update fail");
    }
}
